﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using ColorLife.Core.FileManager;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
public partial class Content_editors_FileManager_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private JavaScriptSerializer _serializer;
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string LoadTree(string path)
    {
        string html = "";
        if (path == "0")
        {
            html += "<li path='/uploads/' class='jstree-closed' id='root2'><a onclick=$.dalatxanh.loadFileFolder('/uploads/') href='#'>" + "Uploads" + "</a></li>";
        }
        else
        {

            string FullPath = Server.MapPath(path);
            // additional securite check to make sure that no operation can done out of the user file
            String userFileFolder = Server.MapPath("/Uploads");
            if (!FullPath.Contains(userFileFolder))
                html += "";
            string[] Files = Directory.GetFiles(FullPath);
            string[] Directorys = Directory.GetDirectories(FullPath);
            if (Files.Length <= 0 && Directorys.Length <= 0)
            {

                html += "<p class=\"align-center\"><b>Thư mục rỗng.</b></p>";
            }
            foreach (string Dir in Directorys)
            {
                html += "<li path='" + (path + System.IO.Path.GetFileName(Dir)) + "/" + "' class='jstree-closed'><a onclick=$.dalatxanh.loadFileFolder('" + (path + System.IO.Path.GetFileName(Dir) + "/").Replace(" ", "%20") + "')  href='#'>" + Path.GetFileName(Dir) + "</a></li>";
            }
        }
        _serializer = new JavaScriptSerializer();
        return _serializer.Serialize(html);
    }
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetTreeData(string path)
    {
        var rootNode = FileManager.Instance.GetJsTreeData(path);
        return rootNode.ToString();
    }
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<FileItem> LoadAllFileFolder(string path, string searchTxt, string sortOrder, int filter)
    {
        string allowedExtensions = "^.jpg|.gif|.png|.jpeg|.rar|.zip|.swf$";// DalatGreen.Core.Entities.Settings.Setting("file.allowfileext");

        var query = from item in FileManager.Instance.All(path, searchTxt, sortOrder, (FileFilter)filter, allowedExtensions)
                    //  orderby item.LastWriteDate
                    select item;
        //if (query.Count() > 250)
        //    return query.ToList().GetRange(0, 250);
        //return query.ToList();

        return query.ToList();
    }
    //[HttpPost]
    //public JsonResult CreateNewFolder(string path, string name)
    //{
    //    bool rs = FileManager.Instance.CreateNewFolder(path, name);
    //    return Json(rs, JsonRequestBehavior.AllowGet);
    //}
    //[HttpPost]
    //public JsonResult DeleteFileFolder(string path)
    //{
    //    bool rs = FileManager.Instance.DeleteFileFolder(path);
    //    return Json(rs, JsonRequestBehavior.AllowGet);
    //}
    //[HttpPost]
    //public JsonResult RenameFileFolder(string oldName, string newName)
    //{
    //    bool rs = FileManager.Instance.RenameFileFolder(oldName, newName);
    //    return Json(rs, JsonRequestBehavior.AllowGet);
    //}
    //[HttpPost]
    //public JsonResult GetTreeData(string path)
    //{
    //    var rootNode = FileManager.Instance.GetJsTreeData(path);
    //    return Json(rootNode);
    //}

    //[HttpPost]
    //public JsonResult MoveData(string source, string target)
    //{
    //    string sourceDirectory = Server.MapPath(source);
    //    string targetDirectory = Server.MapPath(target);

    //    string mess = "";
    //    try
    //    {

    //        Copy(sourceDirectory, targetDirectory);
    //        mess = "OK";
    //    }
    //    catch (Exception ex)
    //    {
    //        mess = ex.Message;
    //    }
    //    return Json(mess);
    //}

    public void Copy(string sourceDirectory, string targetDirectory)
    {
        DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
        DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);

        CopyAll(diSource, diTarget);
    }
    public void CopyAll(DirectoryInfo source, DirectoryInfo target)
    {
        // Check if the target directory exists, if not, create it.
        if (Directory.Exists(target.FullName) == false)
        {
            Directory.CreateDirectory(target.FullName);
        }
        // Copy each file into it's new directory.
        foreach (FileInfo fi in source.GetFiles())
        {
            fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
        }
        // Copy each subdirectory using recursion.
        foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
        {
            DirectoryInfo nextTargetSubDir =
            target.CreateSubdirectory(diSourceSubDir.Name);
            CopyAll(diSourceSubDir, nextTargetSubDir);
        }
    }

}